<?php
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'Abstract.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'Exception.php';

class Payment_Adapter_Alipay extends Payment_Adapter_Abstract
{
    public function __construct($configs=array())
    {
        $this->signType = 'MD5'; 
        $this->configs = $configs;
        $this->extendsionPath = dirname(__FILE__).'/../Extension/Alipay/';
    }

    public function setConfigs()
    {
        if(!$this->configs['securityCode'])
            throw new Payment_Adapter_Exception('安全校验码(security_code)不存在');

        $this->securityCode = $this->configs['security_code'];
        $this->signType = $this->configs['signType'] ? $this->configs['signType'] : 'MD5';

        $this->parameter = array(
            "service"        => "trade_create_by_buyer",  //交易类型
            "partner"        => $this->configs['partner'],         //合作商户号
            "return_url"     => $this->configs['returnUrl'],      //同步返回
            "notify_url"     => $this->configs['notifyUrl'],      //异步返回
            "_input_charset" => $this->configs['inputCharset'],  //字符集，默认为GBK
            "subject"        => $this->configs['subject'],       //商品名称，必填
            "body"           => $this->configs['body'],       //商品描述，必填
            "out_trade_no"   => $this->configs['orderId'],     //商品外部交易号，必填（保证唯一性）
            "price"          => $this->configs['price'] ? $this->configs['price'] "0.01",           //商品单价，必填（价格不能为0）
            "payment_type"   => "1",              //默认为1,不需要修改
            "quantity"       => $this->configs['quantity'] ? $this->configs['quantity'] : "1",              //商品数量，必填
            "logistics_fee"      =>'0.00',        //物流配送费用
            "logistics_payment"  =>'BUYER_PAY',   //物流费用付款方式：SELLER_PAY(卖家支付)、BUYER_PAY(买家支付)、BUYER_PAY_AFTER_RECEIVE(货到付款)
            "logistics_type"     =>'EXPRESS',     //物流配送方式：POST(平邮)、EMS(EMS)、EXPRESS(其他快递)

            "show_url"       => $this->configs['showUrl'],        //商品相关网站
            "seller_email"   => $this->configs['sellerEmail']     //卖家邮箱，必填
        );
    }

    public function createUrl()
    {
        if(!is_file($this->extendsionPath.'alipay_service.php'))
            throw new Payment_Adapter_Exception($this->extendsionPath.'alipay_service.php'.'不存在');

        require_once $this->extendsionPath.'alipay_service.php';
        $this->setConfigs();
        $this->alipay = new alipay_service($this->parameter,$this->securityCode,$this->signType);

        return $this->alipay->create_url();
    }


    public function callback()
    {
        if(!is_file($this->extendsionPath.'alipay_notify.php')) 
            return false;

        require_once $this->extendsionPath.'alipay_notify.php';
        $this->setConfigs();
        $this->alipay = new alipay_notify($this->parameter,$this->configs['securityCode'],$this->configs['signType'],$this->configs['inputCharset'],$this->configs['transport']);

        $verifyResult = $this->alipay->notify_verify();

        if(!$verifyResult) return $this->log();
    }
}
?>
